package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    //添加
    @RequestMapping("/add")
    public Result add(Integer[] permissionIds, Integer[] menuIds, @RequestBody Role role){
        try {
            //根据关键字查询角色
            Role dataRole = roleService.selectRoleByKey(role.getKeyword());
            if(dataRole != null) {
                //查询角色已存在,此关键字不可用
                return new Result(false, "关键字已被使用");
            }
            //添加角色
            roleService.add(permissionIds, menuIds, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加角色失败");
        }
        return new Result(true, "添加角色成功");
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        return pageResult;
    }

    //编辑弹框回显信息
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role = roleService.findById(id);
            return new Result(true, "查询角色信息成功", role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色信息失败");
        }
    }

    @RequestMapping("/update")
    public Result update(Integer[] permissionIds, Integer[] menuIds, @RequestBody Role role) {
        try {
            //根据关键字查询角色
            Role dataRole = roleService.selectRoleByKey(role.getKeyword());
            if(dataRole != null && dataRole.getId() != role.getId()) {
                //查询角色已存在,此关键字不可用
                return new Result(false, "关键字已被使用");
            }
            //添加角色
            roleService.update(permissionIds, menuIds, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改角色失败");
        }
        return new Result(true, "修改角色成功");
    }

    @RequestMapping("/deleteRole")
    public Result deleteRole(Integer id) {
        //删除角色信息
        try {
            //首先查询后台是否有用户使用当前角色
            List<User> user = roleService.findUserByRoleId(id);
            if (user.size() > 0 ) {
                //有用户, 不可删除
                return new Result(false, "无法删除,当前角色正被其他用户所使用");
            }
            //没有用户, 进行删除
            roleService.deleteRoleById(id);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false, "删除角色信息失败");
        }
        return new Result(true, "删除角色成功");
    }
}
